<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar">
      <el-form :inline="true" :model="filters" :size="size" ref="filters" label-width="100px">
        <el-row class="search-input">
          <el-form-item label="币种名称：" prop="currencyName">
            <el-input v-model="filters.currencyName" placeholder="币种名称" @input="change($event)"></el-input>
          </el-form-item>
          <el-form-item label="币种码：" prop="currencyCode">
            <el-input v-model="filters.currencyCode" placeholder="币种码" @input="change($event)"></el-input>
          </el-form-item>
          <el-form-item label="启用/禁用：" prop="enableStatus">
            <el-select v-model="filters.enableStatus" placeholder="启用/禁用">
              <el-option label="全选" value=""></el-option>
              <el-option
                v-for="item in enableStatusOption"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row class="search-button">
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(null)"
            />
            <kt-button
              icon="fa fa-plus"
              :label="$t('action.add')"
              type="primary"
              @click="handleAdd"
            />
            <kt-button icon="fa fa-hand" label="启用/禁用" type="primary" @click="reviewStatus"/>
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-refresh"
              :label="$t('action.reset')"
              type="primary"
              @click="resetForm('filters')"
            />
          </el-form-item>
        </el-row>
        <el-row class="t_r button">
          <el-form-item>
            <el-button-group>
              <el-tooltip content="刷新" placement="top">
                <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
              </el-tooltip>
              <el-tooltip content="列显示" placement="top">
                <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
              </el-tooltip>
              <el-tooltip content="导出" placement="top">
                <el-button icon="fa fa-file-excel-o" @click="downloadExcel"></el-button>
              </el-tooltip>
            </el-button-group>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="toolbar" style="float:right;padding-right:15px;">
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table
      :width="1000"
      :height="250"
      :data="pageResult"
      :columns="filterColumns"
      :showEditOperation="true"
      :showBatchDelete="true"
      :showDeleteOperation="true"
      @findPage="findPage"
      @handleEdit="handleEdit"
      @handleDelete="handleDelete"
      @selectionChange="selectionChange"
      ref="CyTable"
    ></cy-table>
    <!--新增编辑界面-->
    <el-dialog
      :title="operation?'新增':'编辑'"
      width="40%"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      @closed="handleClose"
    >
      <el-form
        :model="dataForm"
        label-width="110px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
        label-position="right"
      >
        <el-row class="searchDialog-input">
          <el-form-item label="ID：" prop="id" v-if="false">
            <el-input
              v-model="dataForm.id"
              :disabled="true"
              auto-complete="off"
              placeholder="请输入ID"
            ></el-input>
          </el-form-item>
          <el-form-item label="币种名称：" prop="currencyName">
            <el-input v-model="dataForm.currencyName" placeholder="币种名称" :disabled="isCNY"></el-input>
          </el-form-item>
          <el-form-item label="币种码：" prop="currencyCode">
            <el-input v-model="dataForm.currencyCode" placeholder="币种码" :disabled="isCNY"></el-input>
          </el-form-item>
          <el-form-item label="币种符号：" prop="currencySign">
            <el-input v-model="dataForm.currencySign" placeholder="币种符号"></el-input>
          </el-form-item>
          <el-form-item label="汇率：" prop="rate">
            <el-input v-model="dataForm.rate" placeholder="请输入人民币：币种的汇率" :disabled="isCNY"></el-input>
          </el-form-item>
           <el-form-item label="本位币" prop="standCurrency">
          <el-select v-model="dataForm.standCurrency" placeholder="请选择" style="width:100%">
            <el-option
              v-for="item in currencyOption"
              :key="item.key"
              :label="item.value"
              :value="item.key">
            </el-option>
          </el-select>
          </el-form-item>
           <el-form-item label="启用/禁用" prop="enableStatus">
          <el-select v-model="dataForm.enableStatus" placeholder="请选择" style="width:100%">
            <el-option
              v-for="item in enableStatusOption"
              :key="item.key"
              :label="item.value"
              :value="item.key">
            </el-option>
          </el-select>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="dialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="editLoading"
        >{{$t('action.submit')}}</el-button>
      </div>
    </el-dialog>
    <el-dialog title="启用/禁用页面" :visible.sync="checkpage" width="30%">
      <el-form
        :model="statusForm"
        ref="statusForm"
        :rules="statusRuleForm"
        :size="size"
        label-width="160px"
      >
        <el-row class="searchDialog-input">
          <el-form-item prop="enableStatus" label="是否启用：">
            <el-select v-model="statusForm.enableStatus" placeholder="请选择">
              <el-option label="启用" :value="1"></el-option>
              <el-option label="禁用" :value="0"></el-option>
            </el-select>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:right">
        <el-button @click="checkpage = false">取 消</el-button>
        <el-button type="primary" @click="checkpaged">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import CyTable from "@/views/Core/JicTable";
import KtButton from "@/views/Core/KtButton";
import { format } from "@/utils/datetime";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
export default {
  components: {
    CyTable,
    KtButton,
    TableColumnFilterDialog
  },
  data() {
    return {
      today: "",
      isCNY: false,
      userid: localStorage.getItem("userId"),
      usename: localStorage.getItem("userName"),
      size: "small",
      filters: {},
      enableStatusOption: [
        {
          key: "1",
          value: "启用"
        },
        {
          key: "0",
          value: "禁用"
        }
      ],
      statusRuleForm: {
        enableStatus: [
          { required: true, message: "请选择状态", trigger: "change" }
        ]
      },

      currencyOption: [
        {
          key: "1",
          value: "是"
        },
        {
          key: "2",
          value: "否"
        }
      ],
      options: {},
      columns: [
        { prop: "currencyName", label: "币种名称", minWidth: 100 },
        { prop: "currencyCode", label: "币种码", minWidth: 80 },
        { prop: "currencySign", label: "币种符号", minWidth: 80 },
        { prop: "rate", label: "汇率", minWidth: 80 },
        {
          prop: "standCurrency",
          label: "是否本位币",
          minWidth: 80,
          formatter: this.statusFormatterCurrency
        },
        {
          prop: "sysPreset",
          label: "系统预设",
          minWidth: 100,
          formatter: this.statusFormatterPreset
        },
        {
          prop: "enableStatus",
          label: "启用/禁用",
          minWidth: 100,
          formatter: this.statusFormattErenable
        },
        { prop: "creatorName", label: "创建人", minWidth: 100 },
        { prop: "createTime", label: "创建时间", minWidth: 100 },
        { prop: "updatorName", label: "编辑人", minWidth: 100 },
        { prop: "updateTime", label: "编辑时间", minWidth: 100 },
        { prop: "enablePor", label: "启用人", minWidth: 100 },
        { prop: "enableTime", label: "启用时间", minWidth: 100 },
        { prop: "disablePor", label: "禁用人", minWidth: 100 },
        { prop: "disableTime", label: "禁用时间", minWidth: 100 }
      ],

      filterColumns: [],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: {},
      checked: [],
      operation: false, // true:新增, false:编辑
      dialogVisible: false,
      detailDialogVisible: false, // 新增编辑界面是否显示
      editLoading: false,
      checkpage: false,
      // 新增编辑界面数据
      dataForm: {
        currencyName: "",
        currencyCode: "",
        currencySign: "",
        rate: "",
        standCurrency: ""
      },
      statusForm: {},
      dataFormRules: {
        currencyName: [
          { required: true, message: "请输入币种名称", trigger: "blur" }
        ],
        currencyCode: [
          { required: true, message: "请输入币种码", trigger: "blur" }
        ],
        currencySign: [
          { required: true, message: "请输入币种符号", trigger: "blur" }
        ],
        rate: [{ required: true, message: "请输入汇率", trigger: "blur" }],
        standCurrency: [
          { required: true, message: "请输入本位币", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 获取分页数据
    findPage: function(data) {
      if (data != null && data.start != null) {
        if (data.start != null) {
          this.pageRequest.pageNum = data.start;
        }
        if (data.limit != null) {
          this.pageRequest.pageSize = data.limit;
        }
      }
      let this_ = this;
      if (this_.filters == undefined || this_.filters == null) {
        this_.filters = {};
      }
      this_.filters.start = this.pageRequest.pageNum;
      this_.filters.limit = this.pageRequest.pageSize;
      this.utils.request.queryCurrencyList(this_.filters, function(res) {
        if (res.data == null) {
          res.data = [];
          res.totle = 0;
        } else {
          this_.$refs.CyTable.findPage({
            content: res.data.rows,
            total: res.data.total
          });
        }
      });
    },
    // 编辑
    submitForm: function() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            var this_ = this;
            let params = Object.assign({}, this.dataForm);
            if (params.id != "" && params.id != undefined) {
              params.updator = this_.userid;
              params.updatorName = this_.usename;
              params.updateTime = new Date();
            }
            params.enableStatus = 1; //默认启用
            params.enableTime = new Date(); //启用时间
            params.sysPreset = 0; //系统预设
            params.standCurrency = 0; //本位币
            params.creator = this_.userid;
            params.creatorName = this_.usename;
            this.utils.request.editCurrencyManage(params, res => {
              if (res.code == "0000") {
                this_.$message({ message: "操作成功", type: "success" });
              } else if (res.code == "9999") {
                this_.$message({ message: "操作失败 ", type: "error" });
              }
              this_.dialogVisible = false;
              this_.findPage();
            });
          });
        }
      });
    },
    //提交禁用
    checkpaged() {
      this.$refs.statusForm.validate(valid => {
        if (valid) {
          let this_ = this;
          let params = Object.assign({}, this.statusForm);
          let query = {};
          if (params.enableStatus == 1) {
            query.enablePor = this_.usename;
            query.enableTime = new Date(); //启用时间
          } else if (params.enableStatus == 0) {
            query.disablePor = this_.usename;
            query.disableTime = new Date();
          }
          query.id = this_.checked[0].id;
          query.enableStatus = params.enableStatus;
          this_.utils.request.editCurrencyManage(query, function(res) {
            if (res.code == "0000") {
              this_.$message({ message: "操作成功", type: "success" });
            } else if (res.code == "9999") {
              this_.$message({ message: "操作失败", type: "error" });
            }
            this_.checkpage = false;
            this_.findPage();
          });
        }
      });
    },
    //input防失效
    change(e) {
      this.$forceUpdate();
    },
    reviewStatus() {
      if (this.checked.length > 1 || this.checked.length < 1) {
        this.$message({ message: "启用/禁用只可选择一条数据", type: "error" });
        return;
      } else {
        this.checkpage = true;
      }
    },
    //关闭
    handleClose() {
      this.isCNY = false;
      this.dataForm = {};
      this.$refs.dataForm.resetFields();
    },
    //复选框
    selectionChange(data) {
      this.checked = data.selections;
    },
    statusFormatterCurrency: function(row, column, cellValue, index) {
      if (cellValue == 1) {
        return "是";
      } else if (cellValue == 0) {
        return "否";
      }
    },
    statusFormatterPreset: function(row, column, cellValue, index) {
      if (cellValue == 1) {
        return "是";
      } else if (cellValue == 0) {
        return "否";
      }
    },
    statusFormattErenable: function(row, column, cellValue, index) {
      if (cellValue == 1) {
        return "启用";
      } else if (cellValue == 0) {
        return "禁用";
      }
    },
    //重置
    resetForm(formName) {
      (this.formName = {
        currencyName: "",
        currencyCode: "",
        enableStatus: ""
      }),
        this.$refs.filters.resetFields();
      this.findPage(null);
    },
    // 显示新增界面
    handleAdd: function() {
      this.dataForm = {
        id: "",
        currencyName: "",
        currencyCode: "",
        currencySign: "",
        rate: "",
        standCurrency: "",
        enableStatus: ""
      };
      this.dialogVisible = true;
      this.operation = true;
    },
    // 显示编辑界面
    handleEdit: function(params) {
      console.log(params);
      if (params.row.sysPreset == 1) {
        this.isCNY = true;
      }
      this.dialogVisible = true;
      this.operation = false;
      this.dataForm = Object.assign({}, params.row);
    },
    // 批量删除
    handleDelete: function(data) {
      var this_ = this;
      var ids = "";
      for (var i = 0; i < data.params.length; i++) {
        ids = ids + data.params[i].id + ",";
      }
      let query = {};
      query.ids = ids;
      this_.utils.request.deleteCurrencyInfo(query, function(res) {
        if (res.code == "0000") {
          this_.$message({ message: "操作成功", type: "success" });
        } else if (res.code == "9999") {
          this_.$message({ message: "操作失败" + res.msg, type: "error" });
        }
        this_.findPage();
      });
    },
    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    // 处理表格列过滤显示
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    // 处理表格列过滤显示
    initColumns: function() {
      var temp = [];

      $.each(this.columns, function(key, val) {
        temp.push(val);
      });
      this.filterColumns = temp;
    },
    //列表下载
    downloadExcel() {
      this.$confirm("确定下载列表文件?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let filename = "币别管理";
          this.$refs.CyTable.exportData(
            this.filters,
            this.filterColumns,
            filename
          );
        })
        .catch(() => {});
    }
  },

  mounted() {
    this.initColumns();
  }
};
</script>

<style scoped lang='scss'>
</style>
